//
//  FFCommonUtil.m
//  LoveAssistant
//
//  Created by NoNo on 16/8/25.
//  Copyright © 2016年 nono. All rights reserved.
//

#import "FFCommonUtil.h"
#import "FFDefine.h"
#import <MJExtension.h>
#import <NSString+MD5.h>

@implementation FFCommonUtil

+ (instancetype)shareUtil
{
   static FFCommonUtil *util;
    static dispatch_once_t oneceToken;
    dispatch_once(&oneceToken, ^{
        util = [[FFCommonUtil alloc]init];
    
    });
    
    return util;
}



- (NSString *)utilCreateDirInDocuments:(NSString *)dirName
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *createPath = [NSString stringWithFormat:@"%@/%@", pathDocuments,dirName];
    
//    LALog(@"%@",createPath);
    // 判断文件夹是否存在，如果不存在，则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
        
    } else {
        NSLog(@"FileDir is exists.");
        
    }
    return createPath;
}

- (NSString *)utilGetCurrentTime:(NSString *)timeFormat
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:timeFormat];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *currentTimeStr = [NSDate date];
    NSString *confromTimespStr = [formatter stringFromDate:currentTimeStr];
    return confromTimespStr;
}

- (void)utilSaveImage:(UIImage *)image path:(NSString *)path
{
    
    dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT , 0 ), ^{
        NSLog(@"%@\n%@",[NSThread currentThread],path);
        [UIImagePNGRepresentation(image)writeToFile: path atomically:YES];
        
    });
}

+ (id)utilJsonStringToDict:(NSString *)jsonString
{
    return [jsonString mj_JSONObject];
}

+ (NSArray *)utilPlistToModel:(NSString *)className plistPath:(NSString *)plistPath
{
    return [[NSClassFromString(className) mj_objectArrayWithFile:plistPath] copy];
}

+ (NSArray *)utilKeyValuesToObjectArray:(NSString *)className sourceArray:(NSArray *)sourceArray
{
    return [[NSClassFromString(className) mj_objectArrayWithKeyValuesArray:sourceArray] copy];
    
}

+ (NSString *)utilGetDocumentPath
{
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
}

+ (NSString *)utilGetLibCachesPath
{
    return NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];
}

+ (NSString *)utilGetLibCachesPathWithFileName:(NSString *)fileName
{
    return [[self utilGetLibCachesPath] stringByAppendingPathComponent:fileName];
}

+ (NSString *)utilCreateDirInLibCaches:(NSString *)dirName
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString *pathDocuments = [self utilGetLibCachesPath];
    NSString *createPath = [NSString stringWithFormat:@"%@/%@", pathDocuments,dirName];
    
    //    LALog(@"%@",createPath);
    // 判断文件夹是否存在，如果不存在，则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
        
    } else {
        NSLog(@"FileDir is exists.");
        
    }
    return createPath;
}

+ (NSString *)utilGetDocumentPathWithFileName:(NSString *)fileName
{
    return [[self utilGetDocumentPath] stringByAppendingPathComponent:fileName];
}

+ (UIImage *)utilCompressImage:(UIImage *)image toMaxFileSize:(NSInteger)maxFileSize compression:(CGFloat)compression
{
    
    CGFloat maxCompression = 0.1f;
    NSData *imageData = UIImageJPEGRepresentation(image, compression);
    NSLog(@"%zd",[imageData length]);
    while ([imageData length] > maxFileSize && compression > maxCompression) {
        
        compression -= 0.1;
        NSLog(@"%lf",compression);
        imageData = UIImageJPEGRepresentation(image, compression);
    }
    
    UIImage *compressedImage = [UIImage imageWithData:imageData];
    return compressedImage;
}

+ (UIImage *)utilCompressImage:(UIImage *)image toMaxFileSize:(NSInteger)maxFileSize
{
    return [self utilCompressImage:image toMaxFileSize:maxFileSize compression:0.9];
}

+ (UIImage *)utilCompressImage:(UIImage *)image compression:(CGFloat)compression
{
    NSData *imageData = UIImageJPEGRepresentation(image, compression);
    UIImage *compressedImage = [UIImage imageWithData:imageData];
    return compressedImage;
    
}

+ (NSString *)utilMd5:(NSString *)string
{
    return [string MD5Digest];
}

+ (NSString *)URLDecodedString:(NSString *)str
{
//    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
    NSString *decodedString= (__bridge_transfer NSString *) CFURLCreateStringByReplacingPercentEscapes(NULL,(__bridge CFStringRef)str,CFSTR(""));
//    NSString * decodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault,(CFStringRef)str,(CFStringRef)@"!*'();:@&=+$,/?%#[]"));
    
    return decodedString;
}

@end
